﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;

namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCThemNCC : UserControl
    {
        NhaCungCapBUO nccBUO = new NhaCungCapBUO();
        String idNhaCungCap;
        public UCThemNCC()
        {
            InitializeComponent();
            dataGridView1.DataSource = nccBUO.GetAll();
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtEmail.Text = "";
            txtNCC.Text = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (nccBUO.SearchByTenNhaCungCap(txtNCC.Text))
            {
                MessageBox.Show("Nhà cung cấp này đã có trong danh sách", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                tbl_NhaCungCap tblncc = new tbl_NhaCungCap();
                tblncc.TenNhaCungCap = txtNCC.Text;
                tblncc.DiaChi = txtDiaChi.Text;
                tblncc.SoDienThoai = txtDienThoai.Text;
                tblncc.Email = txtEmail.Text;
                if (nccBUO.Insert(tblncc))
                {
                    dataGridView1.DataSource = nccBUO.GetAll();
                    MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Thêm không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            refeshThemNCC();
            
        }
        /// <summary>
        /// Load lại form thêm nhà cung cấp
        /// </summary>
        void refeshThemNCC()
        {
            Form form = this.ParentForm;
            UCThemNCC ucThemNCC=new UCThemNCC();
            ControlAccess.ShowConTrol(form, ucThemNCC, "grbHienThiChucNang");
        }
        private void sửaNhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form = this.ParentForm;
            UCSuaNCC ucSuaNCC = new UCSuaNCC(idNhaCungCap);
            ControlAccess.ShowConTrol(form, ucSuaNCC, "grbHienThiChucNang");
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                        idNhaCungCap = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void xóaNhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (nccBUO.Delete(idNhaCungCap))
                {
                    MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = nccBUO.GetAll();
            }
        }
    }
}
